package com.bihu.bihudemo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bihu.bihudemo.entity.Tag;
import com.bihu.bihudemo.entity.TagArticleLink;
import com.bihu.bihudemo.result.Result;
import com.bihu.bihudemo.service.TagArticleLinkService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * (TagArticleLink)表控制层
 *
 * @author makejava
 * @since 2020-07-15 17:10:49
 */
@RestController
@RequestMapping("tagArticleLink")
public class TagArticleLinkController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private TagArticleLinkService tagArticleLinkService;

    /**
     * 分页查询所有数据
     *
     * @param page           分页对象
     * @param tagArticleLink 查询实体
     * @return 所有数据
     */
    @GetMapping
    public R selectAll(Page<TagArticleLink> page, TagArticleLink tagArticleLink) {
        return success(this.tagArticleLinkService.page(page, new QueryWrapper<>(tagArticleLink)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        return success(this.tagArticleLinkService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param tagArticleLink 实体对象
     * @return 新增结果
     */
    @PostMapping
    public R insert(@RequestBody TagArticleLink tagArticleLink) {
        return success(this.tagArticleLinkService.save(tagArticleLink));
    }

    //根据文章的ID新增数据
    @ApiOperation("根据文章id与发过来的标签新增一条DB记录，记录文章对应标签ID")
    @PostMapping("/linkTag")
    public Result insertTagLink(@RequestBody List<Integer>tags,@RequestParam("aid")int aid){
        for(Integer tid:tags){
            TagArticleLink link = new TagArticleLink();
            link.setTid(tid);
            link.setAid(aid);
            tagArticleLinkService.save(link);
        }
        return Result.ok();
    }

    //更新文章和与之关联的标签
    @PutMapping("linkTag")
    public Result updateTagLink(@RequestBody List<Integer>tags,@RequestParam("aid")int aid){


        return Result.ok();
    }

    /**
     * 修改数据
     *
     * @param tagArticleLink 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R update(@RequestBody TagArticleLink tagArticleLink) {
        return success(this.tagArticleLinkService.updateById(tagArticleLink));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public R delete(@RequestParam("idList") List<Long> idList) {
        return success(this.tagArticleLinkService.removeByIds(idList));
    }
}